package com.zy.yun.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.annotation.NotNullPara;
import com.zy.yun.base.common.DataTable;
import com.zy.yun.base.common.RestResult;
import com.zy.yun.base.constant.StatusEnum;
import com.zy.yun.base.entity.DictItem;
import com.zy.yun.base.entity.dto.DictItemDTO;
import com.zy.yun.base.entity.vo.DictItemVO;
import com.zy.yun.controller.common.BaseController;
import com.zy.yun.provider.service.IDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统字典表 前端控制器
 * </p>
 *
 * @author 王彦委
 * @since 2019-09-30
 */
@Api(tags = "系统字典项目管理",description = "系统字典项目管理")
@RestController
@RequestMapping("/dictItem")
public class DictItemController extends BaseController<DictItem, DictItemVO> {


    @Autowired
    private IDictItemService dictItemService;

    /**
     * 功能描述: 系统字典分页查询
     *
     * @auther: wangyanwei
     * @date: 2019/9/29 16:15
     * @param:
     * @return:
     */
    @PostMapping("findPage")
    @ApiOperation("系统字典项目列表")
    public DataTable findPage(DictItem dictItem, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "30") Integer pageSize) {

        EntityWrapper<DictItem> dictItemQueryWrapper = new EntityWrapper<>();
        dictItemQueryWrapper.orderBy("seq");
        dictItemQueryWrapper.eq("dictId", dictItem.getDictId());

        if (StringUtils.isNotBlank(dictItem.getName())) {
            dictItemQueryWrapper.like("name", dictItem.getName());
        }
        return findPage(dictItemQueryWrapper, pageNum, pageSize);

    }

    /**
     * 功能描述: 增加字典项目
     *
     * @auther: wangyanwei
     * @date: 2019/9/30 16:56
     * @param:
     * @return:
     */
    @PostMapping("save")
    @ApiOperation("增加系统字典项目")
    @NotNullPara({"name", "value"})
    public RestResult save(DictItemDTO dictItem) {

        if (StringUtils.isNotBlank(dictItem.getNote()) && dictItem.getNote().length()>50) {
            return RestResult.buildError("备注最多50个字！");
        }

        //判断名称是否存在
        EntityWrapper<DictItem> dictItemQueryWrapper = new EntityWrapper<>();
        dictItemQueryWrapper.eq("name", dictItem.getName());
        List<DictItem> list = dictItemService.selectList(dictItemQueryWrapper);

        if (CollectionUtils.isNotEmpty(list)) {
            return RestResult.buildError("名称已经存在！");
        }


        dictItem.setStatus(StatusEnum.STATUS_START.getCode());
        if (dictItemService.insert(dictItem)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }



    /**
     * @Author: 王彦委
     * @Description: 查询所有的开启状态下的字典项目
     * @Date: 19:36 2019/10/19
     */
    @ApiOperation(value = "根据查询状态查询字典项目")
    @PostMapping("findDictItemByStatus")
    public RestResult findDictItemByStatus(DictItemDTO dictItem) {

        EntityWrapper<DictItem> dictItemQueryWrapper = new EntityWrapper<>();

        if (StringUtils.isNotBlank(dictItem.getStatus())) {
            dictItemQueryWrapper.eq("status", dictItem.getStatus());
        }

        List<DictItem> dictItems = dictItemService.selectList(dictItemQueryWrapper);
        return RestResult.buildSuccess(dictItems);

    }
}
